﻿
using System.Globalization;
using System.Text;
using NoNameGoServer.Client.Extensions;

namespace NoNameGoServer.Client.Commands {
    public class UserNameCommand : Command {
        private readonly string _userName;

        public UserNameCommand(string userName) {
            _userName = userName;
        }

        public override void Execute(NNGSClient nngsClient) {
            // Do nothing, wait for the prompt.
        }

        public override void ProcessReceivedLine(NNGSClient nngsClient, string line, out bool processed) {
            bool lineStartsWithLogin = line.StartsWith("Login:", true, CultureInfo.InvariantCulture);
            bool bufferStartsWithLogin = false;
            if (!lineStartsWithLogin) {
                bufferStartsWithLogin = line.StartsWith("Login:", true, CultureInfo.InvariantCulture);
            }
            if (lineStartsWithLogin || bufferStartsWithLogin) {
                nngsClient.SendAsync(this, string.Format("{0}\n", _userName), null);
                FireCompleted(this, new CompletedArgs());
                processed = true;
                return;
            }
            processed = false;
        }

    }
}
